﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RedditViewer
{
    /// <summary>
    /// Interaction logic for SettingsDialog.xaml
    /// </summary>
    public partial class SettingsDialog : Window
    {
        public SettingsDialog()
        {
            InitializeComponent();
            

            txtUsername.Text = Properties.Settings.Default.Username;
            txtPassword.Password = Properties.Settings.Default.Password;
            txtSaveFolder.Text = Properties.Settings.Default.SaveFolder;
            chkSaveImages.IsChecked = Properties.Settings.Default.SaveImages;
            chkSlideshow.IsChecked = Properties.Settings.Default.Slideshow;
            sldSlideshowInterval.Value = Properties.Settings.Default.SlideshowSpeed;
            txtSubreddits.Text = String.Join("\n", Properties.Settings.Default.Subreddits.Cast<string>());
            chkUseMySubreddits.IsChecked = Properties.Settings.Default.UseSubscribedSubreddits;
            chkSetWallpaper.IsChecked = Properties.Settings.Default.SetWallpaper;

            List<Stretch> arr = new List<Stretch>();
            foreach (var v in Enum.GetValues(typeof(Stretch)))
                ddStretch.Items.Add((Stretch)v);

            ddStretch.SelectedValue = Properties.Settings.Default.Stretch;
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            
            if (this.DialogResult.HasValue && DialogResult.Value == true)
            {
                if (txtUsername.Text.Length > 0 
                    && txtPassword.Password.Length > 0
                    && 
                    (
                        txtUsername.Text != Properties.Settings.Default.Username  ||
                        txtPassword.Password != Properties.Settings.Default.Password
                    )
                   )
                {
                    string error;
                    if (!ApiHelper.Login(txtUsername.Text, txtPassword.Password, out error))
                    {
                        txtPassword.Focus();
                        txtPassword.SelectAll();
                        MessageBox.Show("Login failed: " + error, "Invalid Login", MessageBoxButton.OK);
                        e.Cancel = true;
                        return;
                    }
                }



                Properties.Settings.Default.Username = txtUsername.Text;
                Properties.Settings.Default.Password = txtPassword.Password;
                Properties.Settings.Default.SaveFolder =txtSaveFolder.Text;
                Properties.Settings.Default.SaveImages = chkSaveImages.IsChecked.Value;
                Properties.Settings.Default.Slideshow = chkSlideshow.IsChecked.Value;
                Properties.Settings.Default.SlideshowSpeed = (int)sldSlideshowInterval.Value;
                Properties.Settings.Default.Subreddits = new System.Collections.Specialized.StringCollection();
                Properties.Settings.Default.UseSubscribedSubreddits = chkUseMySubreddits.IsChecked.Value;
                Properties.Settings.Default.Stretch = (Stretch)ddStretch.SelectedValue;
                Properties.Settings.Default.SetWallpaper = chkSetWallpaper.IsChecked.Value;
                foreach(string s in txtSubreddits.Text.Replace("\r", "").Split('\n'))
                {
                    if (s.Length > 0)
                        Properties.Settings.Default.Subreddits.Add(s);
                }
                
                Properties.Settings.Default.Save();
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void chkSlideshow_Checked(object sender, RoutedEventArgs e)
        {
            sldSlideshowInterval.IsEnabled = true;
            lblSlideshowInterval.IsEnabled = true;
        }

        private void chkUseMySubreddits_Checked(object sender, RoutedEventArgs e)
        {
            txtSubreddits.IsEnabled = false;
        }

        private void btnBrowseSaveLocation_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();
            txtSaveFolder.Text = dialog.SelectedPath;
        }

        private void chkSaveImages_Checked(object sender, RoutedEventArgs e)
        {
            btnBrowseSaveLocation.IsEnabled = txtSaveFolder.IsEnabled = true;
        }

        private void chkSaveImages_Unchecked(object sender, RoutedEventArgs e)
        {
            btnBrowseSaveLocation.IsEnabled = txtSaveFolder.IsEnabled = false;
            
        }

        private void chkSlideshow_Unchecked(object sender, RoutedEventArgs e)
        {
            sldSlideshowInterval.IsEnabled = false;
            lblSlideshowInterval.IsEnabled = false;
        }

        private void sldSlideshowInterval_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
                lblSlideshowInterval.Content = (int)sldSlideshowInterval.Value + " seconds";

        }

        private void chkUseMySubreddits_Unchecked(object sender, RoutedEventArgs e)
        {
            txtSubreddits.IsEnabled = true;
        }


        
    }
}
